<?php

namespace app\v1\controller;

use app\v1\model\CountryIntro;
use app\v1\model\Question;
use app\v1\model\QuestionOptionResult as AppQuestionOptionResult;
use think\Controller;
use think\Request;

class QuestionOptionResult extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $questionOptionResult = (new AppQuestionOptionResult())->getAll();
        return view('questionOptionResult/index', compact('questionOptionResult'));
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $question_model = new Question();
        $question = $question_model->getAllOrderByAsc();
        $question2 = $question_model->getAllOrderByAsc(2);
        $question5 = $question_model->getAllOrderByAsc(6);
        $question6 = $question_model->getAllOrderByAsc(7);
        $country_intro_model = new CountryIntro();
        $country_intro = $country_intro_model->getCountryIntro();
        return view('questionOptionResult/create', compact('question','question2','question5','question6', 'country_intro'));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $post = $request->post();
        $question_id = [];
        $option_id = [];
        foreach ($post as $question => $option) {
            if ($question == 'country_intro_id') continue;
            $question_id[] = trim($question, 'option');
            $option_id[] = $option;
        }
        $question_id_concat = '2,6,7';#implode(',', $question_id);
        $option_id_concat = implode(',', $option_id);
        $country_intro_id_concat = implode(',', $post['country_intro_id']);
        $data = compact('question_id_concat','option_id_concat','country_intro_id_concat');
        $res = AppQuestionOptionResult::create($data);
        if ($res) {
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
